package com.google.coffeeshop.gui.desktop.seat;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;
import java.util.Locale;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import vn.bang.utility.DateUtil;

import com.google.coffeeshop.SeatStatus;
import com.google.coffeeshop.business.CodeGenerationBusiness;
import com.google.coffeeshop.business.ManagerBusiness;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.gui.desktop.DatePicker;
import com.google.coffeeshop.gui.desktop.ObservingTextField;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class AddEditSeatFrame extends JFrame{	
	private static final long serialVersionUID = 1L;
	private JTextField txtCode;
	private JTextField txtName;
	private ObservingTextField txtCreatedDate;

	private SeatPanel seatPanel;
	JLabel lblSeatTitle;
	private Long seatID = null;
	private JComboBox<SeatStatus> cbbStatus;
	private JTextArea taDescription;
	private JButton btnAdd;
	/**
	 * @wbp.parser.constructor
	 */
	public AddEditSeatFrame(SeatPanel seatPanel) {
		super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		initLayout();
		this.seatPanel = seatPanel;
		this.cbbStatus.setSelectedItem(SeatStatus.NEW);
		this.txtCreatedDate.setText(DateUtil.toString(DateUtil.getNow()));
		this.txtCode.setText(CodeGenerationBusiness.getSeatCode());
		if (CodeGenerationBusiness.getLastestSeatId() != null) {
			this.txtName.setText(String.valueOf(CodeGenerationBusiness.getLastestSeatId() + 1));
		}
		this.btnAdd.setText("Add");
		setFrameTitle("Add New Seat");
	}
	public AddEditSeatFrame(SeatPanel seatPanel, long seatId) {
		super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		initLayout();
		this.seatID = seatId;
		this.seatPanel = seatPanel;
		this.btnAdd.setText("Update");
		displaySeatDetails(seatID);
		setFrameTitle("Update Seat");
		
	}
	
	private void displaySeatDetails(long seatId) {
		SeatDTO seat = ManagerBusiness.findSeatById(seatId);
		//set data to field here
		txtCode.setText(seat.getCode());
		txtName.setText(seat.getSeatName());
		txtCreatedDate.setText(
				seat.getCreatedDate() != null 
					? DateUtil.toString(seat.getCreatedDate()) 
					: DateUtil.toString(DateUtil.getNow()));
		taDescription.setText(seat.getDescription());
		cbbStatus.setSelectedItem(SeatStatus.getSeatStatus(seat.getStatus()));
	}

	public void maximizeScreen() {
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
	}
	
	public void restoreScreen() {
		this.setExtendedState(JFrame.NORMAL);
	}
	
	public void refreshGUI() {
		this.validate();
		this.repaint();
	}
	
	private void updateOrAddSeat() {
		SeatDTO modifiedSeat = getSeatDTO();
		ManagerBusiness.saveSeat(modifiedSeat);
		refreshParentGUI();
		disposeFrame();
	}
	
	private SeatDTO getSeatDTO() {
		SeatDTO seat = new SeatDTO();
		seat.setId(seatID);
		seat.setCode(txtCode.getText());
		seat.setSeatName(txtName.getText());
		seat.setCreatedDate(DateUtil.toDate(txtCreatedDate.getText()));
		seat.setDescription(taDescription.getText());
		seat.setStatus(((SeatStatus)cbbStatus.getSelectedItem()).getStatusId());
		return seat;

	}
	private void refreshParentGUI() {
		// TODO Auto-generated method stub
		seatPanel.refreshGUI();
	}
	private void disposeFrame() {
		this.dispose();
	}
	private void setFrameTitle(String title){
		lblSeatTitle.setText(title);
	}
	private void initLayout(){
		setSize(600, 335);
		getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(39dlu;default)"),
				ColumnSpec.decode("max(7dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(121dlu;default):grow"),
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(31dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		lblSeatTitle = new JLabel();
		lblSeatTitle.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblSeatTitle.setHorizontalAlignment(SwingConstants.CENTER);
		getContentPane().add(lblSeatTitle, "2, 2, 9, 1, center, default");
		
		JLabel lblCode = new JLabel("Code");
		getContentPane().add(lblCode, "4, 4, right, default");
		
		txtCode = new JTextField();
		getContentPane().add(txtCode, "7, 4, fill, default");
		txtCode.setColumns(10);
		
		JLabel lblName = new JLabel("Name");
		getContentPane().add(lblName, "4, 6, right, default");
		
		txtName = new JTextField();
		getContentPane().add(txtName, "7, 6, fill, default");
		txtName.setColumns(10);
		
		JLabel lblStatus = new JLabel("Status");
		getContentPane().add(lblStatus, "4, 10, right, default");
		
		cbbStatus = new JComboBox<SeatStatus>();
		cbbStatus.setEnabled(false);
		cbbStatus.setModel(new DefaultComboBoxModel<SeatStatus>(SeatStatus.values()));
		getContentPane().add(cbbStatus, "7, 10, fill, default");
		
		
		JLabel lblCreatedDate = new JLabel("Created Date");
		getContentPane().add(lblCreatedDate, "4, 12, right, default");
		
		txtCreatedDate = new ObservingTextField();
		txtCreatedDate.setEditable(false);
		txtCreatedDate.setColumns(10);
		getContentPane().add(txtCreatedDate, "7, 12, fill, default");
		
		JButton btnSelectCreatedDate = new JButton("...");
		btnSelectCreatedDate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				datePicker(txtCreatedDate);
			}
		});
		getContentPane().add(btnSelectCreatedDate, "10, 12");
		
		JLabel lblDescription = new JLabel("Description");
		getContentPane().add(lblDescription, "4, 14, right, default");
		
		taDescription = new JTextArea();
		getContentPane().add(taDescription, "7, 14, 1, 3, fill, fill");
		
		btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateOrAddSeat();
			}
		});
		getContentPane().add(btnAdd, "2, 20, 6, 1, fill, fill");
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				disposeFrame();
			}
		});
		getContentPane().add(btnCancel, "10, 20, fill, fill");
	}
	
	private Locale getLocale(String locate) {
		if(locate!=null && locate.length()>0)
			return new Locale(locate);
		else return Locale.FRANCE;
	}
	
	private void datePicker(ObservingTextField txtDate){
		String lang = null;
		final Locale loc = getLocale(lang);
		DatePicker dp = new DatePicker(txtDate, loc);
		Date selectedDate = dp.parseDate(txtDate.getText());
		dp.setSelectedDate(selectedDate);
		dp.start(txtDate);
	}
	
}
